package com.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youdianda.common.CustomException;
import com.youdianda.common.PageResult;
import com.youdianda.common.R;
import com.youdianda.dto.YddAdvposDto;
import com.youdianda.entity.YddAdvimg;
import com.youdianda.entity.YddAdvpos;
import com.youdianda.service.YddAdvimgService;
import com.youdianda.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class YddAdvposController {

    @Autowired
    private YddAdvposService yddAdvposService;

    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 分页查询广告位
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public R page(String advposdesc,String advposname,int page,int limit){
        //构造分页构造器对象
        Page<YddAdvpos> pageInfo = new Page<>(page,limit);
//        Page<YddAdvposDto> advposDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件,模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(advposdesc),YddAdvpos::getAdvposdesc,advposdesc);
        queryWrapper.like(StringUtils.isNotEmpty(advposname),YddAdvpos::getAdvposname,advposname);
        //添加排序条件
        queryWrapper.orderByDesc(YddAdvpos::getId);
        //执行分页查询
        yddAdvposService.page(pageInfo,queryWrapper);
        log.info("查询到数据");

        List<YddAdvposDto> yddAdvposDtos=pageInfo.getRecords().stream().map(item ->{
            YddAdvposDto yddAdvposDto = new YddAdvposDto();
            BeanUtils.copyProperties(item,yddAdvposDto);
            //查询该广告位下有多少个广告
            LambdaQueryWrapper<YddAdvimg> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(YddAdvimg::getAdvimgpos,item.getId());
            int count = yddAdvimgService.count(queryWrapper1);

            yddAdvposDto.setAdvposnum(count);

            return yddAdvposDto;
        }).collect(Collectors.toList());

//        Page<YddAdvposDto> advposDtoPage1 = advposDtoPage.setRecords(yddAdvposDtos);
//        return R.success(advposDtoPage1);

        //将数据封装为pageResult
         PageResult pageResult = PageResult.getResultByDtoPage(pageInfo, yddAdvposDtos);
        return R.success(pageResult);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/all")
    public R<List<YddAdvpos>> getAll(){
        List<YddAdvpos> list = yddAdvposService.list();
        return R.success(list);
    }

    /**
     * 新增广告位
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/add")
    public R<String> save(@RequestBody YddAdvpos yddAdvpos){
        log.info(yddAdvpos.toString());
        yddAdvposService.save(yddAdvpos);

        return R.success("新增广告成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public R<String> delete(@RequestParam Long id){
        log.info("id{}:",id);
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        if ("1".equals(yddAdvpos.getDefaultData())){
            throw new CustomException("系统默认数据无法删除");
        }

        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddAdvimg::getAdvimgdesc,id);
        int count = yddAdvimgService.count(queryWrapper);
        if (count>0){
            throw new CustomException("该广告下有关联的广告，无法删除");
        }
        yddAdvposService.removeById(id);
        return R.success("广告位删除成功");
    }

    /**
     * 修改数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R<YddAdvpos> rowInfo(@RequestParam Long id){
        log.info("id{}:",id);
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        return R.success(yddAdvpos);
    }

    /**
     * 修改数据
     * @param yddAdvpos
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvpos yddAdvpos){
        yddAdvposService.updateById(yddAdvpos);
        return R.success("数据修改成功");
    }
}
